/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview :
 * @author : diogo <a233894432@gmial.com>
 * @since : 2015年8月5日15:25:12
 * @filename : Gruntfile.js
 * @version :
 * @description : 自动化构建项目
 */

module.exports = function(grunt) {

	//初始化
	grunt.initConfig({
		//读入项目配置信息
		pkg: grunt.file.readJSON('package.json'),

		//合并插件
		concat: {
			options: {
				//定义一个用于插入合并输出文件之间的字符
				separator: ';'
			},
			dist: {
				//合并前的JS文件的存放位置,注意有些JS具有依赖顺序压缩
				//src: ['src/zepto.js', 'src/touch.js', 'src/event.js'],
				src: 'src/resource/js/*.js',
				//可以批理合并
				//合并后的JS文件的存放位置
				//dest: 'dist/<%= pkg.name %>.js'
				dest: 'dist/resource/js/app.js'
			}
		},

		//压缩 
		uglify: {
			options: {
				//此处定义的banner注释将插入到输出文件的顶部

//				banner: '/*! v3.ttq8.com - ' +
//					'<%= grunt.template.today("yyyy-mm-dd") %> */',
				beautify: true, //是否压缩
				mangle: false, //不混淆变量名
				compress: true, //打开或关闭使用默认选项源压缩。
			},
			my_target: {
				files: {
					'dist/resource/js/app.min.js': ['dist/resource/js/app.js']
				}
			}
		},

		//CSS压缩合并
		cssmin: {
			options: {
				//此处定义的banner注释将插入到输出文件的顶部

				banner: '/*! ttq8.com - ' +
					'<%= grunt.template.today("yyyy-mm-dd") %> */'

			},
			target: {
				files: {
					//注意CSS具有先后顺序，覆盖的问题。压缩合并必须数组方式
					'dist/resource/css/style.min.css': ['src/resource/css/style.css']
				}
			}
		},

		//图片压缩
		imagemin: {
			dynamic: {
				options: {
					//优化级别1-7，默认是3，效果不明显                        
					optimizationLevel: 3,
				},
				files: [{
					expand: true,
					cwd: "src/resource/img/",
					src: ['*.{png,jpg,gif}'],
					dest: "dist/resource/img/"
				}]
			}
		},
		//html 压缩
		htmlmin: { // Task 
			dist: { // Target 
				options: { // Target options 
					removeComments: true, //删除注解
					collapseWhitespace: false //删除空格
				},
				files: { // Dictionary of files 
					'dist/index.html': 'src/index.html' // 'destination': 'source' 
				}
			},
			dev: { // Another target 
				files: {
					'dist/index.html': 'src/index.html' // 'destination': 'source' 
				}
			}
		},
		//html插入js
		'sails-linker': {
			defaultOptions: {
				options: {
					startTag: '<!--SCRIPTS-->',
					endTag: '<!--SCRIPTS END-->',
					fileTmpl: '<script src="%s"></script>',
					appRoot: 'dist/'
				},
				files: {
					// Target-specific file lists and/or options go here. 
					'dist/index.html': ['dist/zepto/*.js']
				},
			},
		},
		/*sass: { // Task 
					dist: { // Target 
						options: { // Target options 
							style: 'expanded'
						},
						files: { // Dictionary of files 
							'resource/css/main.css': 'resource/css/scss/*.scss'
						}
					}
		},*/
		//watch插件的配置信息,监听测试不是每次修改都监听的到？还需要在测试
		watch: {
			html: {
				files: 'src/index.html',
				tasks: ['htmlmin:dist']
			},
			css: {
				files: 'src/resource/css/*.css',
				tasks: ['cssmin']

			}

		},
		//copy 复制目录 /文件
		copy: {
			main: {
				expand: true,
				cwd: 'src/lib/',
				src: '**',
				dest: 'dist/lib/',
				//flatten: true,
				//filter: 'isFile',
			},
		}

	});
	// 加载提供任务的插件
	grunt.loadNpmTasks('grunt-contrib-uglify');
	grunt.loadNpmTasks('grunt-contrib-concat');
	grunt.loadNpmTasks('grunt-contrib-cssmin');
	grunt.loadNpmTasks('grunt-contrib-imagemin');
	grunt.loadNpmTasks('grunt-contrib-htmlmin');
	grunt.loadNpmTasks('grunt-contrib-qunit');
	grunt.loadNpmTasks('grunt-contrib-jshint');
	grunt.loadNpmTasks('grunt-contrib-watch');
	grunt.loadNpmTasks('grunt-contrib-clean');
	grunt.loadNpmTasks('grunt-contrib-copy');
	grunt.loadNpmTasks('grunt-sails-linker');
	grunt.loadNpmTasks('grunt-newer');
	// 默认任务
	grunt.registerTask('default', ['concat', 'uglify', 'cssmin', 'imagemin','htmlmin:dist','coy']);
	grunt.registerTask('build', ['concat', 'uglify', 'cssmin', 'imagemin', 'htmlmin:dev', 'sails-linker']);
	grunt.registerTask('watching', ['watch']);
	grunt.registerTask('coy', ['copy']);
	//	grunt.registerTask('test', ['jshint', 'qunit']);
	//  grunt.registerTask('watch',['watch']);
	//grunt.registerTask('default', ['concat','uglify','cssmin','imagemin','watch']);
	//grunt.registerTask('default', ['concat','uglify','cssmin','newer:imagemin','watch']);
}